package View;

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Controller.Sender;

@SuppressWarnings("serial")
public class Screen extends JFrame
{
	private String START = "Start";
	
	
	private JPanel pnlMain;
	private JButton btnStart;
	private JTextField tbxMessage;
	private JTextField tbxPushToken;
	private JTextField tbxPullToken;
	
	private JLabel lblMessageTitle;
	private JLabel lblPushToken;
	private JLabel lblPullToken;
	
	private JLabel lblPulledMessage;
	private JLabel lblPulledMessageTitle;
	
	private Sender m_Sender;
	
	byte[] pushToken;
	byte[] pullToken;
	int index;
	String[] data;
	
	public Screen(Sender sender) 
	{
		super();
		
		m_Sender = sender;
		
		pnlMain = new JPanel(new GridLayout(0,1));
		
		btnStart = new JButton(START);
		
		tbxMessage = new JTextField();
		tbxPullToken = new JTextField();
		tbxPushToken = new JTextField();
		
		lblPullToken = new JLabel();
		lblPushToken = new JLabel();
		lblMessageTitle = new JLabel();
		
		lblPulledMessage = new JLabel();		
		lblPulledMessageTitle = new JLabel();
		
		lblMessageTitle.setText("Message:");
		lblPullToken.setText("Pull token:");
		lblPushToken.setText("Push token:");
		tbxMessage.setText("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
		tbxPullToken.setText(m_Sender.getMyPublicToken());
		tbxPushToken.setText(m_Sender.getMyPublicToken());
		lblPulledMessageTitle.setText("Message pulled: ");
		
		 
		setBounds(100,100,300,300);
	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    Container con = this.getContentPane();
	    con.add(pnlMain);
	    pnlMain.add(lblPushToken);
	    pnlMain.add(tbxPushToken);
	    pnlMain.add(lblPullToken);
	    pnlMain.add(tbxPullToken);
	    
	    pnlMain.add(lblMessageTitle);
	    pnlMain.add(tbxMessage);	    
	    
	   
	    pnlMain.add(btnStart);
	    pnlMain.add(lblPulledMessageTitle);
	    pnlMain.add(lblPulledMessage);
	    	    
	    btnStart.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				pushToken = tokenFromString(tbxPushToken.getText());
				pullToken = tokenFromString(tbxPullToken.getText());
				
				data = tbxMessage.getText().split(" ");				
				
				Timer timer = new Timer();
				timer.scheduleAtFixedRate(new TimerTask() {
					
					@Override
					public void run() {
						
						String msg = "";
						if (index < data.length)
						{
							msg = data[index++];
						}
						
						String response = m_Sender.pushPull(pushToken, msg, pullToken);
						if (!response.equals(""))
						{
							lblPulledMessage.setText(lblPulledMessage.getText() + " " + response);
						}
					}
					
				}, 0, 100);				
			}
		});
	    setVisible(true); 
	}	
	
	private byte[] tokenFromString(String t)
	{
		String[] temp = t.split(" ");
		byte[] token = new byte[temp.length];
		int i = 0;
		for (String s : temp)
		{
			token[i++] = Byte.parseByte(s);
		}
		
		return token;
	}
}
